/* TotalDcmListerPluginInterface.cpp -- Defines the exported functions for the DLL application.
*
* TotalDcmLister - A lister plugin for Total Commander for listing DICOM File content.
*
* Copyright (C) 2015 Captain Stark
*
* This software may be modified and distributed under the terms of the MIT license.
* See the COPYRIGHT file at the top-level directory of this distribution for details.
*/

#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers

#include "stdafx.h"

#include <string>
#include <CommCtrl.h>

#include "TotalDcmListerPluginInterface.h"
#include "TotalDcmListerPlugin.h"

//*** Global declarations **********************************************************************************************
TotalDcmListerPlugin plugin;

//*** Plugin method definitons *****************************************************************************************
/// <summary>
/// ListGetDetectString is called when the plugin is loaded for the first time. 
/// It should return a parse function which allows Lister to find out whether your plugin can probably handle the file 
/// or not. You can use this as a first test - more thorough tests may be performed in ListLoad(). 
/// It's very important to define a good test string, especially when there are dozens of plugins loaded! 
/// The test string allows lister to load only those plugins relevant for that specific file type.
/// </summary>
/// <param name="detectString">The detection string.</param>
/// <param name="maxlen">Maximum length, in bytes, of the detection string (currently 2k).< / param>
void APIENTRY ListGetDetectString(char* detectString, int maxlen)
{
   std::string extensions = "EXT=DCM\0";

   strcpy_s(detectString, maxlen, extensions.c_str());
}

/// <summary>
/// ListSetDefaultParams is called immediately after loading the DLL, before ListLoad. 
/// This function is new in version 1.2. It requires Total Commander >=5.51, but is ignored by older versions.
/// </summary>
/// <param name="dps">
/// This structure of type ListDefaultParamStruct currently contains the version number of the plugin interface, 
/// and the suggested location for the settings file (ini file). It is recommended to store any plugin-specific 
/// information either directly in that file, or in that directory under a different name. 
/// Make sure to use a unique header when storing data in this file, because it is shared by other file system plugins! 
/// If your plugin needs more than 1kbyte of data, you should use your own ini file because ini files are limited to 64k.
/// </param>
void APIENTRY ListSetDefaultParams(ListDefaultParamStruct* dps)
{
   if (nullptr != dps)
   {
      plugin.SetSettingsLocation(dps->DefaultIniName);
   }
}

/// <summary>
/// ListLoad is called when a user opens lister with F3 or the Quick View Panel with Ctrl+Q, and when the definition 
/// string either doesn't exist, or its evaluation returns true.
/// </summary>
/// <param name="parentWin">This is lister's window. Create your plugin window as a child of this window.< / param>
/// <param name="fileToLoad">The name of the file which has to be loaded.</param>
/// <param name="showFlags">The show flags.</param>
/// <returns>A handle to lister window if load succeeds, NULL otherwise.< / returns>
HWND APIENTRY ListLoad(HWND parentWin, char* fileToLoad, int showFlags)
{
   if (plugin.InitializePluginView(parentWin))
   {
      if (plugin.LoadDicomFileContent(fileToLoad))
      {
         return plugin.GetPluginViewHandle();
      }
   }

   return NULL;
}

/// <summary>
/// ListCloseWindow is called when a user closes lister, or loads a different file.
/// </summary>
/// <param name="listView">This is the window handle which needs to be destroyed.< / param>
void APIENTRY ListCloseWindow(HWND listView)
{
   DestroyWindow(listView);
   return;
}